package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.log4j.Logger;

public class UsualUtil {

	private static Logger LOG = Logger.getLogger(UsualUtil.class);

	public static boolean emptyOrNot(String str) {
		if (null != str.trim() && !str.trim().equals(""))
			return true;
		return false;
	}

	public static boolean isNumeric(String str) {
		for (int i = str.length(); --i >= 0;) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	public static String ToDBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248);
		}

		return new String(c);
	}

	public static String formateFolderPath(String folderPath) {
		StringBuilder sBuilder = new StringBuilder(folderPath.replaceAll("\\",
				"/"));
		return sBuilder.toString();
	}

	public static String translateASCII(String str) {
		char[] chars = str.toCharArray();
		StringBuilder strASCII = new StringBuilder();
		for (int i = 0; i < chars.length; i++) {
			strASCII.append("#" + (int) chars[i] + ";");
		}
		return strASCII.toString();
	}

	public static String translateString(String accii) {
		StringBuilder strGroup = new StringBuilder();
		String[] chars = accii.replace("#", "").split(";");
		for (int i = 0; i < chars.length; i++) {
			strGroup.append((char) Integer.parseInt(chars[i]));
		}
		return strGroup.toString();
	}

	public static String escape(String src) {
		int i;
		char j;
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length() * 6);

		for (i = 0; i < src.length(); i++) {

			j = src.charAt(i);

			if (Character.isDigit(j) || Character.isLowerCase(j)
					|| Character.isUpperCase(j))
				tmp.append(j);
			else if (j < 256) {
				tmp.append("%");
				if (j < 16)
					tmp.append("0");
				tmp.append(Integer.toString(j, 16));
			} else {
				tmp.append("%u");
				tmp.append(Integer.toString(j, 16));
			}
		}
		return tmp.toString();
	}

	public static String unescape(String src) {
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length()) {
			pos = src.indexOf("%", lastPos);
			if (pos == lastPos) {
				if (src.charAt(pos + 1) == 'u') {
					ch = (char) Integer.parseInt(
							src.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				} else {
					ch = (char) Integer.parseInt(
							src.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			} else {
				if (pos == -1) {
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				} else {
					tmp.append(src.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}
}